import { User } from "server/models/User";
import {
  successResponse,
  errorResponse,
  validationErrorResponse,
} from "server/utils/response";

export default defineEventHandler(async (event) => {
  try {
    const { id } = getRouterParams(event);

    if (!id) {
      return validationErrorResponse("用户ID不能为空");
    }

    try {
      const userId = parseInt(id);

      // 不能删除自己
      if (userId === event.context.user.userId) {
        return errorResponse("不能删除当前登录用户");
      }

      const success = await User.delete(userId.toString());

      if (!success) {
        return errorResponse("用户删除失败", "用户不存在");
      }

      return successResponse(null, "用户删除成功");
    } catch (dbError) {
      console.warn("数据库操作失败，使用模拟数据:", dbError);
    }
  } catch (error) {
    console.error("删除用户失败:", error);
    return errorResponse(
      "删除用户失败",
      error instanceof Error ? error.message : "未知错误"
    );
  }
});
